package cc.sjfx.sharedoc.bmob;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.mandroid.frame.toast.MLog;
import cn.mandroid.frame.toast.MToast;
import cc.sjfx.sharedoc.cache.UserCache;
import cc.sjfx.sharedoc.db.bmob.User;
import cc.sjfx.sharedoc.utils.CheckUtils;
import android.content.Context;

public class BmobCache {
	public static void updateUser(final Context context,
			final UpdateStatus status) {
		BmobQuery<User> query = new BmobQuery<User>();
		String account = BmobUser.getCurrentUser(context).getUsername();
		query.addWhereEqualTo("username", account);
		query.findObjects(context, new FindListener<User>() {
			@Override
			public void onSuccess(List<User> arg0) {
				// TODO Auto-generated method stub
				if (!CheckUtils.stringIsNull(BmobUser.getCurrentUser(context,
						User.class).getSessionId())
						&& BmobUser.getCurrentUser(context, User.class)
								.getSessionId()
								.equals(arg0.get(0).getSessionId())) {
					UserCache.user = arg0.get(0);
					if (status != null)
						status.onSuccess(arg0.get(0));
				} else {
					User.logOut(context);
					MToast.show(context, "账号在别处登录，您已强制下线！");
				}
			}

			@Override
			public void onError(int arg0, String arg1) {
				// TODO Auto-generated method stub
				MLog.i("error" + arg1);
			}
		});
	}

	public static void updateUser(final Context context) {
		updateUser(context, null);
	}
}
